package edu.cque.hello.util;

import java.sql.*;

/**
 * @author Hua
 * @create 2022-03-25 21:14
 */
public class Db {

    /**
     * 封装了方法,用来获取数据库的连接
     *
     * @return
     */
    public static Connection getConnection() throws Exception {
        Connection connection = null;
        //1, 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获取数据库的连接
        //String url ="jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false";
        String url = "jdbc:mysql://localhost:3307/cgb2107?useSSL=false";
        connection = DriverManager.getConnection(url, "root", "root");
        return connection;
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (resultSet != null) {

            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
